Hey there.
Thanks for making it to this lecture.
This lecture is going to be a little short as we're just going to talk about what polymorphism is,
but we won't really practice any examples of polymorphism in Lua as there is no practical use of this
concept in Lua.
Nor is there any good example that I can give you in Lua, as its effects only exist because of inheritance,
which we've already learned about.
So I'm just going to give you a simple explanation of polymorphism, and this is an idea that you can
carry along with you if you learn some object oriented language like C plus, plus or C sharp.
Anyways, polymorphism is an object oriented programming concept that allows objects of different classes
to be treated as if they were of the same class or in other words, have classes, inherit the same
functions, yet act differently.
How is this achieved?
Well, the classes would use a common interface or a set of methods known as an abstract class containing
abstract functions.
An object oriented programming languages.
There's a dedicated way of doing this, but we can't really do it in Lua.
How it's typically done is by creating what we would call an abstract class, because it doesn't really
stand for anything, and the only purpose of it is to contain abstract functions that can be commonly
shared among other classes.
In fact, an abstract class doesn't even contain any implementation for any functions.
The only purpose of an abstract class is just to contain a blueprint or a contract for what a class
should contain, and from that point, the classes that inherit those blueprinted functions have to
create the functionality for it themselves.
So here's an example of polymorphism that I got working.
But at the end of the day it's actually just inheritance.
Anyways, so I've created this quote unquote abstract class because we can't actually make any in Lua.
But what this abstract class is doing is defining some functions that some shapes like a circle class
or a rectangle class or whatever, should have if we were to make those classes.
So these classes should have a constructor, and they should have a draw function to define the position
of the shape inside of these there is no functionality, it just errors.
Just in case, maybe the class that is inheriting these blueprinted or abstract functions hasn't implemented
functionality for these functions yet, so it's just going to error and say that this function has not
been implemented.
And then what I've done here is I've created a circle class.
Set the index meta method to itself so we can access these functions.
And then I've set the meta table for the circle class to the shape class.
That means if we were to ever index any functions inside of the circle class that did not exist, but
they existed within the shape class, it'll look into the shape class, see that this function exists,
and then it will error saying, hey, this class here hasn't implemented any functionality for, let's
say, the constructor or this draw function.
So inside of these functions you know, I've implemented some stuff.
It doesn't just error.
When I create a new circle we set the meta table to the circle class.
We set the x, the y and the radius.
And then we just return it.
And then for the draw function, it just prints the location of the circle and it tells us the radius
of the circle.
So when I create this new circle here has a position on the x axis of three, on the y axis of five,
and it has a radius of ten.
And when I go to draw the function it should print that right on the console for us.
So when I hit run.
There we go.
We have a circle at X of three, y of five, and a radius of ten.
But let's say I didn't implement this draw function.
What if I were to just comment out and this draw function didn't exist within the circle class?
Well, what's going to happen is that the interpreter is going to check for this draw, um, index here.
It's going to look inside of this object and see it has this meta table of circle.
It's going to go in here and see if the circle class has the draw function which it doesn't because
I commented it out.
Then it'll see okay.
Well this circle class does have another meta table which is shape which is inheritance by the way.
And it'll look inside the shape for an index meta method which it has.
So it'll check this table and see that the function exists in here.
And because we didn't put any implementation within the circle class for the draw function, it's just
going to error and tell us that this function has not been implemented.
So if we go and run the game.
There we go.
We get an error and it says draw function has not yet been implemented.
And we can also do the same thing for the constructor.
If we were to comment out the constructor and run the game, it'll tell us a new function has not yet
been implemented.
So the only purpose of this shape class is to be an abstract class that's telling us what our shape
classes like for circles, or for rectangles or whatever, that they should have these functions in
them by default.
And if they don't, it's going to error and tell them that this function has not been implemented.
But realistically, there is no practical use of polymorphism in Lua.
The only reason why this is working is because we're using inheritance.
So in short, you don't need to worry about polymorphism, but it's going to be an important concept.
You'll want to learn about it for other languages that you might want to learn in the future.
Otherwise, that's all for this lecture and I'll see you in the next one.